<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cursor Token Login</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      body {
        padding: 12px;
        font-family: var(--vscode-font-family);
        color: var(--vscode-foreground);
        background-color: var(--vscode-editor-background);
      }

      .container {
        max-width: 800px;
        margin: 0 auto;
        display: flex;
        flex-direction: column;
        gap: 12px;
      }

      .section {
        background: var(--vscode-editor-background);
        border: 1px solid var(--vscode-panel-border);
        border-radius: 6px;
        padding: 14px;
        transition: all 0.2s;
      }

      .section:hover {
        border-color: var(--vscode-focusBorder);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }

      .section-title {
        font-size: 14px;
        font-weight: 600;
        margin-bottom: 10px;
        padding-bottom: 8px;
        border-bottom: 1px solid var(--vscode-panel-border);
        display: flex;
        align-items: center;
        gap: 6px;
      }

      .section-title::before {
        content: "●";
        color: var(--vscode-textLink-foreground);
      }

      label {
        font-size: 12px;
        font-weight: 500;
        margin-bottom: 6px;
        display: block;
        opacity: 0.9;
      }

      input,
      textarea {
        width: 100%;
        padding: 8px 10px;
        background: var(--vscode-input-background);
        color: var(--vscode-input-foreground);
        border: 1px solid var(--vscode-input-border);
        border-radius: 4px;
        font-size: 12px;
        font-family: "Consolas", "Courier New", monospace;
        transition: all 0.2s;
      }

      input:focus,
      textarea:focus {
        outline: none;
        border-color: var(--vscode-focusBorder);
        box-shadow: 0 0 0 3px rgba(14, 99, 156, 0.1);
      }

      input[readonly] {
        opacity: 0.6;
        cursor: not-allowed;
      }

      textarea {
        resize: vertical;
        min-height: 70px;
        line-height: 1.4;
      }

      .button-group {
        display: flex;
        flex-direction: column;
        gap: 8px;
        margin-top: 10px;
      }

      button {
        width: 100%;
        padding: 10px 16px;
        background: var(--vscode-button-background);
        color: var(--vscode-button-foreground);
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-size: 12px;
        font-weight: 500;
        transition: all 0.2s;
        position: relative;
        overflow: hidden;
      }

      button:hover {
        background: var(--vscode-button-hoverBackground);
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
      }

      button:active {
        transform: translateY(0);
      }

      button.danger {
        background: transparent;
        color: var(--vscode-errorForeground);
        border: 1px solid var(--vscode-errorForeground);
      }

      button.danger:hover {
        background: var(--vscode-errorForeground);
        color: white;
        border-color: var(--vscode-errorForeground);
      }

      .form-group {
        margin-bottom: 12px;
      }

      .form-group:last-child {
        margin-bottom: 0;
      }

      .danger-zone {
        border-color: var(--vscode-panel-border);
        background: transparent;
      }

      .info-text {
        font-size: 11px;
        opacity: 0.6;
        margin-top: 4px;
        font-style: italic;
      }

      @keyframes fadeIn {
        from {
          opacity: 0;
          transform: translateY(10px);
        }
        to {
          opacity: 1;
          transform: translateY(0);
        }
      }

      .section {
        animation: fadeIn 0.3s ease-out;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <!-- 用户管理 -->
      <div class="section">
        <div class="section-title">用户切换</div>
        <div class="form-group">
          <label>当前用户</label>
          <div
            id="currentUserDisplay"
            style="
              padding: 8px 10px;
              background: var(--vscode-input-background);
              border: 1px solid var(--vscode-input-border);
              border-radius: 4px;
              font-size: 12px;
              color: var(--vscode-textLink-foreground);
              min-height: 32px;
              display: flex;
              align-items: center;
            "
          >
            <span style="opacity: 0.6">加载中...</span>
          </div>
        </div>
        <div class="form-group">
          <label>用户信息 (JSON 格式)</label>
          <textarea
            id="userInfoInput"
            rows="6"
            placeholder='{
  "email": "your@email.com",
  "token": "cookie_token"
}'
          ></textarea>
          <div class="info-text">输入邮箱和访问令牌以快速切换用户</div>
        </div>
        <div class="button-group">
          <button onclick="getAccessToken()">👤 切换用户</button>
        </div>
      </div>

      <!-- 配置路径 -->
      <div class="section">
        <div class="section-title">Cursor 配置路径</div>
        <div class="form-group">
          <input type="text" id="baseDirInput" value="" placeholder="自动检测..." />
          <div class="info-text">通常位于: C:\Users\你的用户名\AppData\Roaming\Cursor</div>
        </div>
        <div class="button-group">
          <button onclick="resetMachineId()">🔄 重置机器ID</button>
          <button onclick="cleanAugment()">🧹 清理Augment</button>
        </div>
      </div>

      <!-- 危险操作 -->
      <div class="section danger-zone">
        <div class="section-title">⚠️ 危险操作</div>
        <div class="form-group">
          <div class="info-text" style="color: var(--vscode-errorForeground); margin-bottom: 8px">
            以下操作会永久删除文件夹，请谨慎操作！
          </div>
          <div class="button-group">
            <button class="danger" onclick="deleteHistory()">🗑️ 删除 history</button>
            <button class="danger" onclick="deleteWorkspaceStorage()">
              🗑️ 删除 workspaceStorage
            </button>
          </div>
        </div>
      </div>
    </div>

    <script>
      console.log("=== dashboard.html JavaScript 已加载 ===")
      const vscode = acquireVsCodeApi()
      console.log("vscode API 已获取:", vscode)

      // 统一的消息提示函数
      function showMessage(message, type = "info") {
        vscode.postMessage({
          type: "showMessage",
          data: { message, messageType: type }
        })
      }

      // 页面加载时请求路径信息和当前用户
      window.addEventListener("DOMContentLoaded", () => {
        console.log("请求路径信息")
        vscode.postMessage({ type: "getPaths" })
        console.log("请求当前用户信息")
        vscode.postMessage({ type: "getCurrentUser" })
      })

      // 监听来自扩展的消息
      window.addEventListener("message", event => {
        const message = event.data
        console.log("收到消息:", message)

        if (message.type === "paths") {
          console.log("设置路径:", message.data)
          document.getElementById("baseDirInput").value = message.data.baseDir
        } else if (message.type === "currentUser") {
          console.log("当前用户:", message.data)
          const displayEl = document.getElementById("currentUserDisplay")
          if (message.data && message.data.email) {
            displayEl.innerHTML = `<span>📧 ${message.data.email}</span>`
          } else {
            displayEl.innerHTML = '<span style="opacity: 0.6;">未登录</span>'
          }
        } else if (message.type === "accessTokenResult") {
          // 2.得到accessToken，这里的消息查看：帮助 => 查看开发人员工具
          console.log("回传 AccessToken 结果:", message.data)
          if (message.data?.success && message.data?.accessToken) {
            changeUser(message.data)
          }
        }
      })

      // 1.从webview发送获取token消息
      function getAccessToken() {
        console.log("getAccessToken() 函数被调用")
        const baseDir = document.getElementById("baseDirInput").value
        const userInfoJson = document.getElementById("userInfoInput").value
        console.log("🚀 - userInfoJson:", userInfoJson)

        if (!baseDir) {
          showMessage("请输入配置目录", "warning")
          return
        }

        if (!userInfoJson) {
          showMessage("请输入用户信息", "warning")
          return
        }

        try {
          const userInfo = JSON.parse(userInfoJson)
          if (!userInfo.email || !userInfo.token) {
            showMessage("JSON格式错误：需要包含email和token字段", "error")
            return
          }

          console.log("发送获取 AccessToken 消息")
          vscode.postMessage({
            type: "getAccessToken",
            data: {
              email: userInfo.email,
              cookieToken: userInfo.token
            }
          })

          // 清空输入框
          // document.getElementById("userInfoInput").value = ""
        } catch (e) {
          showMessage("JSON格式错误：" + e.message, "error")
        }
      }

      // 3.调用切换用户
      function changeUser(userInfo) {
        console.log("changeUser() 函数被调用")
        const baseDir = document.getElementById("baseDirInput").value

        if (!baseDir) {
          showMessage("请输入配置目录", "warning")
          return
        }

        try {
          console.log("发送修改用户消息:", userInfo)
          vscode.postMessage({
            type: "changeUser",
            data: {
              baseDir: baseDir,
              email: userInfo.email,
              token: userInfo.accessToken
            }
          })

          // 清空输入框
          document.getElementById("userInfoInput").value = ""
        } catch (e) {
          showMessage("JSON格式错误：" + e.message, "error")
        }
      }

      function resetMachineId() {
        console.log("resetMachineId() 函数被调用")
        const baseDir = document.getElementById("baseDirInput").value
        if (!baseDir) {
          showMessage("请输入配置目录", "warning")
          return
        }
        const paths = {
          baseDir: baseDir
        }
        console.log("发送重置消息，baseDir:", baseDir)
        vscode.postMessage({ type: "resetMachineId", paths: paths })
      }

      function cleanAugment() {
        console.log("cleanAugment() 函数被调用")
        const baseDir = document.getElementById("baseDirInput").value
        if (!baseDir) {
          showMessage("请输入配置目录", "warning")
          return
        }
        console.log("发送清理Augment消息，baseDir:", baseDir)
        vscode.postMessage({
          type: "cleanAugment",
          data: { baseDir: baseDir }
        })
      }

      function deleteWorkspaceStorage() {
        console.log("deleteWorkspaceStorage() 函数被调用")
        const baseDir = document.getElementById("baseDirInput").value
        if (!baseDir) {
          showMessage("请输入配置目录", "warning")
          return
        }
        console.log("发送删除workspaceStorage消息")
        vscode.postMessage({
          type: "deleteDirectory",
          data: { baseDir: baseDir, dirType: "workspaceStorage" }
        })
      }

      function deleteHistory() {
        console.log("deleteHistory() 函数被调用")
        const baseDir = document.getElementById("baseDirInput").value
        if (!baseDir) {
          showMessage("请输入配置目录", "warning")
          return
        }
        console.log("发送删除history消息")
        vscode.postMessage({
          type: "deleteDirectory",
          data: { baseDir: baseDir, dirType: "history" }
        })
      }

      function deleteGlobalStorage() {
        console.log("deleteGlobalStorage() 函数被调用")
        const baseDir = document.getElementById("baseDirInput").value
        if (!baseDir) {
          showMessage("请输入配置目录", "warning")
          return
        }
        console.log("发送删除globalStorage消息")
        vscode.postMessage({
          type: "deleteDirectory",
          data: { baseDir: baseDir, dirType: "globalStorage" }
        })
      }

      console.log("=== 所有函数已定义 ===")
    </script>
  </body>
</html>
